package br.ufrj.ppgi.infovis.forms;

import org.mt4j.components.MTComponent;
import org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;

/**
 * Class containing commonly used utility methods for removing and adding
 * listeners and processors, (dis-)allowing event bubbling etc. * 
 */
public class ToolsEventHandling {
    /* ***********Constructors*********** */
    /**
     * Private constructor. Doesn't allow instances of this class.
     */
    private ToolsEventHandling() {
        //
    }

    /* ***********Class methods*********** */
    /**
     * Unregisters all ScaleProcessors from a given component and removes all
     * scale gesture listeners.
     * 
     * @param component
     *            the component
     */
    public static void removeScaleProcessorsAndListeners(MTComponent component) {
        for (AbstractComponentProcessor ip : component.getInputProcessors()) {
            if (ip instanceof ScaleProcessor) {
                component.unregisterInputProcessor(ip);
            }
        }
        component.removeAllGestureEventListeners(ScaleProcessor.class);
    }

    /**
     * Unregisters all DragProcessors from a given component and removes all
     * drag gesture listeners.
     * 
     * @param component
     *            the component
     */
    public static void removeDragProcessorsAndListeners(MTComponent component) {
        for (AbstractComponentProcessor ip : component.getInputProcessors()) {
            if (ip instanceof DragProcessor) {
                component.unregisterInputProcessor(ip);
            }
        }
        component.removeAllGestureEventListeners(DragProcessor.class);
    }

    /**
     * Unregisters all RotateProcessors from a given component and removes all
     * rotate gesture listeners.
     * 
     * @param component
     *            the component
     */
    public static void removeRotateProcessorsAndListeners(MTComponent component) {
        for (AbstractComponentProcessor ip : component.getInputProcessors()) {
            if (ip instanceof RotateProcessor) {
                component.unregisterInputProcessor(ip);
            }
        }
        component.removeAllGestureEventListeners(RotateProcessor.class);
    }

    /**
     * Allows bubbled events for all processors on a given component.
     * 
     * @param component
     *            the component
     */
    public static void allowBubbledEventsForAllProcessors(MTComponent component) {
        for (AbstractComponentProcessor ap : component.getInputProcessors()) {
            if (ap instanceof AbstractCursorProcessor) {
                ((AbstractCursorProcessor) ap).setBubbledEventsEnabled(true);
            }
        }
    }

    /**
     * Disallows bubbled events for all processors on a given component.
     * 
     * @param component
     *            the component
     */
    public static void disallowBubbledEventsForAllProcessors(MTComponent component) {
        for (AbstractComponentProcessor ap : component.getInputProcessors()) {
            if (ap instanceof AbstractCursorProcessor) {
                ((AbstractCursorProcessor) ap).setBubbledEventsEnabled(false);
            }
        }
    }
}
